//
//  LGDeviceSendDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/9.
//

#import "LGDeviceSendDetailController.h"
#import "LGDeviceSendTypeView.h"
#import "LGDeviceSendNumberView.h"
#import "LGDeviceSendAdressView.h"
#import "LGDeviceSendLogisticsView.h"
@interface LGDeviceSendDetailController ()
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGDeviceSendTypeView *typeView;
/**   */
@property (nonatomic , strong)LGDeviceSendNumberView *numberView;
/**   */
@property (nonatomic , strong)LGDeviceSendAdressView *adressView;
/**   */
@property (nonatomic , strong)LGDeviceSendLogisticsView *logisticsView;
/**   */
@property (nonatomic , strong)UIButton *sendBtn;

@end

@implementation LGDeviceSendDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"设备发货";
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.typeView];
    [self.scrollView addSubview:self.numberView];
    [self.scrollView addSubview:self.adressView];
    [self.scrollView addSubview:self.logisticsView];
    [self.view addSubview:self.sendBtn];
    CGFloat height = (kMaxY(self.logisticsView.frame)>Screen_H-topBarHeight-viewPix(68))?kMaxY(self.logisticsView.frame)+viewPix(30):(Screen_H-topBarHeight-viewPix(68));
    self.scrollView.contentSize = CGSizeMake(Screen_W, height);
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/address" parameters:@{@"receipt_id":self.receptid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            //0:邮寄 、 1:线下领取
            self.type = LGNSString(returnData[@"receipt_type"]);
            self.adressView.dataDic = returnData[@"address"]?returnData[@"address"]:[NSDictionary dictionary];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setType:(NSString *)type{
    _type = type;
    if([type isEqualToString:@"0"]){
        //邮寄
        self.typeView.postBtn.selected = YES;
        self.typeView.offlineBtn.selected = NO;
        self.adressView.hidden = NO;
        self.logisticsView.hidden = NO;
    }else{
        //线下领取
        self.typeView.postBtn.selected = NO;
        self.typeView.offlineBtn.selected = YES;
        self.adressView.hidden = YES;
        self.logisticsView.hidden = YES;
    }
}


-(void)typeButtonAction:(UIButton *)sender{
    if(sender == self.typeView.postBtn){
        self.type = @"0";
    }else{
        self.type = @"1";
    }
}

//确认发货
-(void)sendDeviceAction{
    NSString *deviceNum = (self.numberView.contentTF.text.length>0)?self.numberView.contentTF.text:@"";
    if(deviceNum.length == 0){
        [TooltipView showMessage:@"请填写设备编号" offset:0];
        return;
    }
    NSString *logistName = (self.logisticsView.nameTF.text.length>0 && [self.type isEqualToString:@"0"])?self.logisticsView.nameTF.text:@"";
    NSString *logistNum = (self.logisticsView.numberTF.text.length>0 && [self.type isEqualToString:@"0"])?self.logisticsView.numberTF.text:@"";
    
    [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/shipping" parameters:@{@"receipt_id":self.receptid,@"receipt_type":self.type,@"equipment_sn":deviceNum,@"express":logistName,@"waybill":logistNum} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            if(self.deviceSendSuccess){
                self.deviceSendSuccess();
            }
            [TooltipView showMessage:@"发货成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}


-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(68)-bottomSafeBarHeight/3.0)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(LGDeviceSendTypeView *)typeView{
    if(!_typeView){
        _typeView = [[LGDeviceSendTypeView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        [_typeView.postBtn addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_typeView.offlineBtn addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _typeView;
}

-(LGDeviceSendNumberView *)numberView{
    if(!_numberView){
        _numberView = [[LGDeviceSendNumberView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.typeView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(94))];
    }
    return _numberView;
}

-(LGDeviceSendAdressView *)adressView{
    if(!_adressView){
        _adressView = [[LGDeviceSendAdressView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.numberView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(152))];
    }
    return _adressView;
}

-(LGDeviceSendLogisticsView *)logisticsView{
    if(!_logisticsView){
        _logisticsView = [[LGDeviceSendLogisticsView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.adressView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(200))];
    }
    return _logisticsView;
}

-(UIButton *)sendBtn{
    if(!_sendBtn){
        _sendBtn = [UIButton buttonWithTitle:@"确认发货" topMargin:kMaxY(self.scrollView.frame)];
        [_sendBtn addTarget:self action:@selector(sendDeviceAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
